package com.qianfeng.xinyihezi.xinyihezi.adapter.adapter_meng;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.model.bean_meng.User;
import com.qianfeng.xinyihezi.xinyihezi.utils.utils_meng.GlideCircleTransform;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/11/15.
 */

public class MyUserAdapter extends BaseAdapter {
    private List<User> mList;
    private Context mContext;

    public MyUserAdapter(List<User> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(mContext, R.layout.item_user, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(mList.get(i).time + "000")));
        holder.mTextViewTime.setText(format);
        holder.mTextViewName.setText(mList.get(i).name);
        Glide.with(mContext).load(mList.get(i).avatar)
                .transform(new GlideCircleTransform(mContext))
                .into(holder.mImageView);
        return view;
    }

    public static class ViewHolder {
        ImageView mImageView;
        TextView mTextViewName, mTextViewTime;

        public ViewHolder(View view) {
            mImageView = (ImageView) view.findViewById(R.id.iv_user_image);
            mTextViewName = (TextView) view.findViewById(R.id.tv_user_name);
            mTextViewTime = (TextView) view.findViewById(R.id.tv_user_time);
        }
    }
}
